/*
 * @Description: 
 * @Autor: 商晓彬
 * @Date: 2021-08-20 09:56:50
 * @LastEditors: 商晓彬
 * @LastEditTime: 2021-12-18 20:42:37
 */
import { Component, EventEmitter, Injector, Input, Output, ViewChild } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { BomMakeContentServiceProxy } from '@shared/service-proxies/service-proxies';
import { ModalDirective } from 'ngx-bootstrap';
import * as _ from 'lodash';
import { finalize } from 'rxjs/operators';
import { MessageService } from '@abp/message/message.service';
import { Table } from 'primeng/components/table/table';
import { Paginator } from 'primeng/components/paginator/paginator';
import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent';
import { HttpClient } from '@angular/common/http';

@Component({
    selector: 'app-add-bom',
    templateUrl: './add-bom.component.html',
    styleUrls: ['./add-bom.component.less']
})
export class AddBomComponent extends AppComponentBase {
    //引入子组件
    @ViewChild('createOrEditModal') modal: ModalDirective;
    @ViewChild('dataTable') dataTable: Table;
    @ViewChild('paginator') paginator: Paginator;
    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();
    saving = false;
    active = false;
    selectedCar: any;
    model = undefined;
    sendTime = undefined;
    lotNo = undefined;
    saveFlag = true;
    constructor(
        injector: Injector,
        private _messageService: MessageService,
        private _httpClient: HttpClient,
        private _bomMakeContentServiceProxy: BomMakeContentServiceProxy,

    ) {
        super(injector);
    }

    ngOnInit() {
    }
    //
    show(): void {
        this.active = true;
        this.modal.show();
    }
    reset() {
        this.model = undefined;
        this.sendTime = undefined;
        this.lotNo = undefined;
    }
    //
    getTableList(event?: LazyLoadEvent) {
        let that = this;
        if (that.primengTableHelper.shouldResetPaging(event)) {
            that.paginator.changePage(0);
            return;
        }
        that.primengTableHelper.showLoadingIndicator();
        that._bomMakeContentServiceProxy.getBopDescList(
            this.model,
            undefined,
            this.sendTime,
            this.lotNo,
            that.primengTableHelper.getSorting(that.dataTable),
            that.primengTableHelper.getMaxResultCount(that.paginator, event),
            that.primengTableHelper.getSkipCount(that.paginator, event)
        ).pipe(finalize(() => that.primengTableHelper.hideLoadingIndicator())).subscribe(result => {
            that.primengTableHelper.totalRecordsCount = result.totalCount;
            that.primengTableHelper.records = result.items;
            that.primengTableHelper.hideLoadingIndicator();
        });


    }
    //关闭事件
    close(): void {
        this.active = false;
        this.selectedCar = undefined;
        this.modal.hide();
    }
    //导入工艺规程
    save() {
        if (!this.selectedCar) {
            this._messageService.info('请选择');
            return;
        }
        abp.ui.setBusy();
        if (this.saveFlag) {
            this.saveFlag = false;
            this._bomMakeContentServiceProxy.synchronizationBom(this.selectedCar)
                .pipe(finalize(() => { this.saving = false; }))
                .subscribe((result) => {
                    abp.ui.clearBusy();
                    this.saveFlag = true;
                    if (result.code == 0) {
                        this._messageService.success('同步成功');
                        this.close();
                        this.modalSave.emit(null);
                    } else {
                        this._messageService.error(result.message)
                    }
                });
        }

    }
}
